package cn.iocoder.yudao.module.bpm.service.tohandle.flow.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.bpm.controller.admin.tohandle.flow.vo.BpmHandleFlowCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.tohandle.flow.vo.BpmHandleFlowExportReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.tohandle.flow.vo.BpmHandleFlowPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.tohandle.flow.vo.BpmHandleFlowUpdateReqVO;
import cn.iocoder.yudao.module.bpm.convert.tohandle.flow.BpmHandleFlowConvert;
import cn.iocoder.yudao.module.bpm.dal.dataobject.tohandle.BpmHandleFlowDO;
import cn.iocoder.yudao.module.bpm.dal.mysql.tohandle.BpmHandleFlowMapper;
import cn.iocoder.yudao.module.bpm.service.tohandle.flow.BpmHandleFlowService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.HANDLE_FLOW_NOT_EXISTS;

/**
 * 审批流待办中心——流程flow表 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BpmHandleFlowServiceImpl implements BpmHandleFlowService {

    @Resource
    private BpmHandleFlowMapper handleFlowMapper;

    @Override
    public Long createHandleFlow(BpmHandleFlowCreateReqVO createReqVO) {
        // 插入
        BpmHandleFlowDO handleFlow = BpmHandleFlowConvert.INSTANCE.convert(createReqVO);
        handleFlowMapper.insert(handleFlow);
        // 返回
        return handleFlow.getId();
    }

    @Override
    public void updateHandleFlow(BpmHandleFlowUpdateReqVO updateReqVO) {
        // 校验存在
        validateHandleFlowExists(updateReqVO.getId());
        // 更新
        BpmHandleFlowDO updateObj = BpmHandleFlowConvert.INSTANCE.convert(updateReqVO);
        handleFlowMapper.updateById(updateObj);
    }

    @Override
    public void deleteHandleFlow(Long id) {
        // 校验存在
        validateHandleFlowExists(id);
        // 删除
        handleFlowMapper.deleteById(id);
    }

    private void validateHandleFlowExists(Long id) {
        if (handleFlowMapper.selectById(id) == null) {
            throw exception(HANDLE_FLOW_NOT_EXISTS);
        }
    }

    @Override
    public BpmHandleFlowDO getHandleFlow(Long id) {
        return handleFlowMapper.selectById(id);
    }

    @Override
    public List<BpmHandleFlowDO> getHandleFlowList(Collection<Long> ids) {
        return handleFlowMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BpmHandleFlowDO> getHandleFlowPage(BpmHandleFlowPageReqVO pageReqVO) {
        return handleFlowMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BpmHandleFlowDO> getHandleFlowList(BpmHandleFlowExportReqVO exportReqVO) {
        return handleFlowMapper.selectList(exportReqVO);
    }

}
